const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const ExtractTextPlugin = require("extract-text-webpack-plugin")
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

const config = {
  entry: {
    app: [
      path.join(__dirname, 'app/js/viewport.js'),
      path.join(__dirname, 'app/js/main.js')
    ]
  },
  module: {
    loaders: [{
      test: /\.html$/,
      loader: 'html-loader'
    }, {
      test: /\.vue$/,
      loader: 'vue-loader',
      options: {
        loaders: {
          css: 'vue-style-loader!css-loader!px2rem-loader?remUnit=40&remPrecision=8',
          scss: 'vue-style-loader!css-loader!px2rem-loader?remUnit=40&remPrecision=8!sass-loader',
        },
        cssModules: {
          localIdentName: '[path][name]---[local]---[hash:base64:5]',
          camelCase: true
        }
      }
    }, {
      test: /\.scss$/,
      loader: 'style-loader!css-loader!sass-loader'
    }]
  },
  resolve: {
    extensions: [".js", ".json"],
    alias: {
      'vue$': 'vue/dist/vue.esm.js' // 用 webpack 1 时需用 'vue/dist/vue.common.js'
    },
  },
  devtool: 'source-map',
  devServer: {
    contentBase: path.join(__dirname, "dist"),
    hot: true,
    compress: true, //开启gzip压缩
    port: 9000,
    clientLogLevel: "none",
    quiet: true
  },
  plugins: [
    new CleanWebpackPlugin(['dist']),
    new HtmlWebpackPlugin({
      title: 'development',
      template: path.join(__dirname, 'app/views/index.html'),
    }),
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin(),
  ],
  output: {
    filename: '[name].min.js',
    path: path.resolve(__dirname, 'dist')
  }
}

module.exports = env => {
  if (!env) {
    env = {}
  }
  if (env.production) {
    config.plugins.push(
      new webpack.DefinePlugin({
        'process.env': {
          NODE_ENV: '"production"'
        }
      }),
      new ExtractTextPlugin("style.css", {
        ignoreOrder: true
      }),
      new webpack.optimize.ModuleConcatenationPlugin(),
      new UglifyJsPlugin({
        sourceMap: true
      })
    )
    config.module.loaders[1].options.loaders = {
      css: ExtractTextPlugin.extract({
        use: 'css-loader?minimize!px2rem-loader?remUnit=40&remPrecision=8',
        fallback: 'vue-style-loader' // <- 这是vue-loader的依赖，所以如果使用npm3，则不需要显式安装
      }),
      scss: ExtractTextPlugin.extract({
        use: 'css-loader?minimize!px2rem-loader?remUnit=40&remPrecision=8!sass-loader',
        fallback: 'vue-style-loader' // <- 这是vue-loader的依赖，所以如果使用npm3，则不需要显式安装
      }),
    }
  }
  return config
}
